package com.umbacos.udm.client.arch;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;
import com.umbacos.udm.client.model.View;

public class MinMax extends HorizontalPanel {

	private Widget prevParent;
	private Popup pp;

	public MinMax (final View tv){
		MinMaxLabel lbMax=new MinMaxLabel("O", "Open");
		MinMaxLabel lbMin=new MinMaxLabel("X", "Minimize");
		MinMaxLabel lbUp=new MinMaxLabel("<bold>&uarr;</bold>", "Move up");
		MinMaxLabel lbDn=new MinMaxLabel("<bold>&darr;</bold>", "Move down");
		MinMaxLabel lbDetach=new MinMaxLabel(new ImageButton("images/pin.png", 100, 10, 16).toString(), "Pin/Unpin");
		
		setSpacing(5);
		add(lbDetach);
		add(lbUp);
		add(lbDn);
		add(lbMax);
		add(lbMin);

		lbDetach.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if (prevParent == null) {
					prevParent = tv.getParent();
					tv.removeFromParent();
					pp=new Popup("Popup", tv, Popup.POPUP_NOBUTTONS);
					pp.setGlassEnabled(false);
					pp.setModal(false);
					pp.center();
				} else {
					tv.removeFromParent();
					pp.hide();
					pp=null;
					((VerticalPanel)prevParent).add(tv);
					prevParent=null;
				}

				event.stopPropagation();
			}
		});
		
		lbUp.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				tv.moveUp(); 
				event.stopPropagation();
			}
		});
		
		lbDn.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				tv.moveDown(); 
				event.stopPropagation();
			}
		});
		
		lbMax.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				tv.resize(tv.getOffsetWidth(), 200);
				event.stopPropagation();
			}
		});
		
		lbMin.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				tv.resize(tv.getOffsetWidth(), 0);
				event.stopPropagation();
			}
		});		
	}
	
	class MinMaxLabel extends HTML {
		public MinMaxLabel(String html, String title){
			setStyleName("minmax");
			setTitle(title);
			setHTML(html);
			
			addMouseOverHandler(new MouseOverHandler() {
				@Override
				public void onMouseOver(MouseOverEvent event) {
					setStyleName("minmaxover");
				}
			});
			addMouseOutHandler(new MouseOutHandler() {
				
				@Override
				public void onMouseOut(MouseOutEvent event) {
					setStyleName("minmax");
				}
			});
		}
	}
}
